<template>
  <div class="app-container">
    <div class="head-container">
      <el-form :inline="true" :model="queryParams" ref="queryForm" size="small">
        <el-form-item label="车牌号码" prop="carNumber">
          <el-input
            v-model="queryParams.carNumber"
            placeholder="请输入车牌号码"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="缴费状态" prop="paymentStatus">
          <el-select
            v-model="queryParams.paymentStatus"
            placeholder="缴费状态"
            clearable
            style="width: 120px"
          >
            <el-option label="未缴费" value="0" />
            <el-option label="已缴费" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <el-table
      v-loading="loading"
      :data="carList"
      border
      style="width: 100%"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="carNumber" label="车牌号码" width="120" />
      <el-table-column prop="entryTime" label="入场时间" width="180" :formatter="dateFormat" />
      <el-table-column prop="exitTime" label="出场时间" width="180" :formatter="dateFormat" />
      <el-table-column prop="parkingDuration" label="停车时长" width="120" />
      <el-table-column prop="amount" label="应缴费金额(元)" width="150" />
      <el-table-column prop="paymentStatus" label="缴费状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.paymentStatus === 1" type="success">已缴费</el-tag>
          <el-tag v-else type="danger">未缴费</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.paymentStatus === 0"
            type="primary"
            icon="el-icon-money"
            size="mini"
            @click="handlePayment(scope.row)"
          >
            缴费
          </el-button>
          <el-button
            v-else
            type="default"
            icon="el-icon-check"
            size="mini"
            disabled
          >
            已缴费
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    
    <!-- 缴费对话框 -->
    <el-dialog :title="'车辆缴费 - ' + selectedCar.carNumber" :visible.sync="paymentDialogVisible" width="400px" append-to-body>
      <div class="payment-info">
        <p><span class="info-label">车牌号码：</span>{{ selectedCar.carNumber }}</p>
        <p><span class="info-label">入场时间：</span>{{ parseTime(selectedCar.entryTime) }}</p>
        <p><span class="info-label">出场时间：</span>{{ parseTime(selectedCar.exitTime) }}</p>
        <p><span class="info-label">停车时长：</span>{{ selectedCar.parkingDuration }}</p>
        <p><span class="info-label">应缴费金额：</span><span class="amount-text">{{ selectedCar.amount }} 元</span></p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="paymentDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmPayment">确认缴费</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getExitCars, payParkingFee } from '@/api/parking/payment'
import Pagination from '@/components/Pagination'

export default {
  name: 'PaymentChannel',
  components: {
    Pagination
  },
  data() {
    return {
      loading: false,
      carList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNumber: '',
        paymentStatus: ''
      },
      paymentDialogVisible: false,
      selectedCar: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getExitCars(this.queryParams).then(response => {
        this.carList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    handlePayment(car) {
      this.selectedCar = Object.assign({}, car)
      this.paymentDialogVisible = true
    },
    confirmPayment() {
      payParkingFee(this.selectedCar.id, this.selectedCar.amount).then(response => {
        this.$message.success('缴费成功')
        this.paymentDialogVisible = false
        this.getList()
      }).catch(error => {
        this.$message.error('缴费失败，请重试')
      })
    },
    dateFormat(row, column) {
      const date = row[column.property]
      if (date === undefined) {
        return ''
      }
      return this.parseTime(date)
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.head-container {
  margin-bottom: 20px;
}

.payment-info {
  padding: 20px 0;
}

.payment-info p {
  margin-bottom: 15px;
  line-height: 1.5;
}

.info-label {
  display: inline-block;
  width: 100px;
  text-align: right;
  margin-right: 10px;
  color: #606266;
}

.amount-text {
  color: #f56c6c;
  font-size: 18px;
  font-weight: bold;
}
</style>